package ait.mediabroadcast.mediaserver;

import java.net.InetAddress;
import java.util.UUID;

import org.teleal.cling.binding.annotations.AnnotationLocalServiceBinder;
import org.teleal.cling.model.DefaultServiceManager;
import org.teleal.cling.model.ValidationException;
import org.teleal.cling.model.meta.DeviceDetails;
import org.teleal.cling.model.meta.DeviceIdentity;
import org.teleal.cling.model.meta.LocalDevice;
import org.teleal.cling.model.meta.LocalService;
import org.teleal.cling.model.meta.ManufacturerDetails;
import org.teleal.cling.model.meta.ModelDetails;
import org.teleal.cling.model.types.DeviceType;
import org.teleal.cling.model.types.UDADeviceType;
import org.teleal.cling.model.types.UDN;

import ait.mediabroadcast.pauseresume.PauseResumeService;
import android.util.Log;

public class MediaServer {

	private final String LOGTAG = MediaServer.class.toString();
	private LocalDevice mLocalDevice;
	private final static int port = 9000;
	private static InetAddress mLocalAddress;

	public MediaServer(InetAddress localAddress) {
		try {
			mLocalDevice = createDevice();
		} catch (ValidationException e) {
			e.printStackTrace();
		}

		mLocalAddress = localAddress;

		try {
			Log.d(LOGTAG, mLocalAddress.toString());
			new HttpServer(port);
		} catch (Exception e) {
			Log.d(LOGTAG, e.getMessage());
			System.exit(-1);
		}
	}

	LocalDevice createDevice() throws ValidationException {
		
//		DeviceIdentity identity = new DeviceIdentity(UDN.uniqueSystemIdentifier("Own phone here"));
//		UDN udn = new UDN(UUID.fromString(UUID.fromString(Secure.getString(getContext().getContentResolver(),
//            Secure.ANDROID_ID))));
//		UDN udn = new UDN(UUID.randomUUID());
		
		UDN udn = UDN.uniqueSystemIdentifier("MediaBroadcast-MediaServer");
		DeviceIdentity identity = new DeviceIdentity(udn);
		DeviceType type = new UDADeviceType("MediaServer", 1);
		DeviceDetails details = new DeviceDetails(android.os.Build.MODEL,
				new ManufacturerDetails(android.os.Build.MANUFACTURER), 
				new ModelDetails("modelName", "modelDescription", "modelNumber"));
		
		LocalService<ContentDirectoryService> contentDirectoryService = 
				new AnnotationLocalServiceBinder().read(ContentDirectoryService.class);
		contentDirectoryService.setManager(new DefaultServiceManager<ContentDirectoryService>(
				contentDirectoryService, ContentDirectoryService.class));
		
		LocalService<PauseResumeService> pauseResumeService = 
				new AnnotationLocalServiceBinder().read(PauseResumeService.class);
		pauseResumeService.setManager(new DefaultServiceManager<PauseResumeService>(
				pauseResumeService, PauseResumeService.class));
		
		LocalService [] services = {contentDirectoryService, pauseResumeService};
		
		LocalDevice mylocalDevice = new LocalDevice(identity, type, details, services);
		return mylocalDevice;
	}

	public LocalDevice getLocalDevice() {
		return mLocalDevice;
	}

	public String getAddress() {
		return mLocalAddress.getHostAddress() + ":" + port;
	}
}
